/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.uniqueness;

import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.ImmutableCell;
import com.onegravity.sudoku.solver.util.HtmlLoader;

public class UniquenessTestType1Hint extends UniquenessTestHint {

	private static final long serialVersionUID = -648249216852226811L;
	
	private final Cell target;

    public UniquenessTestType1Hint(List<Cell> loop, int v1, int v2, Map<Cell, BitSet> removablePotentials, Cell target) {
        super(loop, v1, v2, removablePotentials);
        this.target = target;
    }

    @Override
    public Collection<Cell> getSelectedCells(HintType hintType) {
    	switch(hintType) {
    	case SMALL_CLUE: return super.getSelectedCells(hintType);
    	default:
    	case BIG_CLUE: return null;
    	case HINT: return Collections.singletonList(target);
    	}
    }

    @Override
    public Map<Cell, BitSet> getSelectedPotentials(HintType hintType, int viewNum) {
        return (hintType!=HintType.SMALL_CLUE) ? super.getSelectedPotentials(hintType, viewNum) : null;
    }

    @Override
    public Map<Cell, BitSet> getSecondarySelectedPotentials(HintType hintType, int viewNum) {
        Map<Cell, BitSet> result = new HashMap<Cell, BitSet>();
        BitSet p = (BitSet) target.getPotentialValues().clone();
        p.clear(v1);
        p.clear(v2);
       	result.put(target, p);
        return (hintType==HintType.HINT) ? result : null;
    }

    @Override
    public Map<ImmutableCell, BitSet> getRemovedPotentials(HintType hintType) {
        BitSet removable = new BitSet(10);
        removable.set(v1);
        removable.set(v2);
        return (hintType==HintType.HINT) ? Collections.singletonMap(new ImmutableCell(target), removable) : null;
    }

    @Override
    protected int getType() {
        return 1;
    }
    
    @Override
    protected HintResource getResource() {
        return HintResource.UNIQUENESS_TEST_1;
    }
    
    @Override
    public String getHintHtml(HintType hintType) {
        String result = HtmlLoader.loadHtml(HintResource.UNIQUENESS_TEST_1.hint(hintType));
        String type = getTypeName();
        String cellName = target.toString();
        Cell[] cells = new Cell[loop.size()];
        loop.toArray(cells);
        String allCells = Cell.toString(cells);
        result = HtmlLoader.format(result, type, v1, v2, allCells, cellName, loop.size());
        return result;
    }

}